package ru.spb.osll.myfit.server.chart;

import ru.spb.osll.myfit.util.FileHelper;

import java.io.*;

/**
 * User: Artem Bukhonov
 * Date: 22.04.12
 * Time: 17:03
 */
public class PngChartWriter implements ChartWriter {
  private final static String IMAGE_FILE_EXT = "png";

  private String m_outputDirectory;

  public PngChartWriter(String outputDirectory) {
    m_outputDirectory = outputDirectory;
  }

  @Override
  public void write(ChartRenderData data, ChartSaveInfo info) throws IOException {
    String imageFilename = info.getIndexInTest() + FileHelper.SEPARATOR + info.getChartName() + "." + IMAGE_FILE_EXT;
    File file = new File(m_outputDirectory, imageFilename);
    OutputStream stream = new FileOutputStream(file);
    ChartRenderer renderer = new BasePngChartRenderer();
    byte[] pictureData = renderer.render(data);
    stream.write(pictureData);
    stream.flush();
    stream.close();
  }
}
